<template>
  <div class="article-container">
    <!--
        导航栏；
        $router.back()返回上一个页面；
        left-arrow：是否显示左侧箭头，默认false，
                    当标签填写left-arrow属性时，值为true；
        fixed：是否固定在顶部；
        $route.query.keyword：获取上一个路由跳转时携带过来的参数，不是$router
    -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loadingIcon" class="article-loading"/>
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <!--
            fit="fill"：图片填充方式为fill
            round：是否显示为圆形
        -->
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo"/>
        <div class="text">
          <p class="name">yoyo</p>
          <!-- 相对时间处理：使用之前定义的全局过滤器 -->
          <p class="time">{{ article.pubdate | relative }}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="clickFocusOn"
        >{{ article.is_followed ? '取关' : '+ 关注' }}
        </van-button>
      </div>
      <!-- 文章正文是html格式字符串，需要用v-html才能正确显示 -->
      <div class="content" v-html="article.content">
      </div>
      <!-- 分隔符，用来分割内容 -->
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small" hairline type="primary" plain
                    :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
                    @click="giveLike"
        >{{ article.attitude === 1 ? '取消点赞' : '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain
                    icon="delete"
                    @click="clickDislike"
        >{{ article.attitude === 0 ? '取消不喜欢' : '不喜欢' }}
        </van-button>
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <!-- 子组件无法通过路由直接获取父组件接收的上一个页面的参数，这里只能用父子通信 -->
    <Comment :artId="$route.params.id"></Comment>
    <!-- /文章评论 -->
  </div>
</template>

<script>

  // push时传对象, params (写name传递) 就通过 this.$route.params获取
  // push时传对象, query  (写path)     就通过 this.$route.query 获取
  // 动态路由传参  /article/:id        this.$route.params 获取

  import { reqGetArticleDetail, reqDeleteLike, reqAddLike, reqAddDisLike, reqDeleteDisLike } from '@/api/article.js'
  import { reqFollowUser, reqUnFollowUser } from '@/api/user.js'

  import Comment from '@/views/article/components/comment'

  export default {
    // article和html标签重复，命名会报错，换个其他不影响的名字
    name: 'articleIndex',
    components: { Comment },
    data () {
      return {
        loadingIcon: true, // 控制加载中的 loading 状态
        article: {}
      }
    },
    created () {
      // 页面初始化调用加载文章详情的方法
      this.loadArticle()
    },
    methods: {
      // 获取文章详情
      async loadArticle () {
        // 动态路由传参  /article/:id        this.$route.params 获取
        const res = await reqGetArticleDetail(this.$route.params.id)
        this.article = res.data.data
        // 关闭van-loading效果
        this.loadingIcon = false
      },

      // 点击关注/取消关注
      async clickFocusOn () {
        try { // 如果article.is_followed值为false 走关注的业务, 反之取关
          if (this.article.is_followed) {
            // 取关
            await reqUnFollowUser(this.article.aut_id)
          } else {
            // 关注
            await reqFollowUser(this.article.aut_id)
          }
          // 直接更新视图即可
          this.article.is_followed = !this.article.is_followed
          this.$toast('操作成功')
        } catch (e) {
          this.$toast('操作失败')
        }
      },

      // 点击取消不喜欢/喜欢
      async clickDislike () {
        try { // 根据attitude来决定调用哪个方法, 如果是0, 说明是不喜欢, 点击就要取消不喜欢
          if (this.article.attitude === 0) {
            await reqDeleteDisLike(this.article.art_id.toString())
            this.article.attitude = -1
          } else {
            await reqAddDisLike(this.article.art_id.toString())
            this.article.attitude = 0
          }
          this.$toast.success('操作成功')
        } catch (e) {
          this.$toast('操作失败')
        }
      },

      // 点赞/取消点赞
      async giveLike () {
        try { // 根据attitude来决定调用哪个方法
          if (this.article.attitude === 1) {
            // 点击时, 要取消点赞
            await reqDeleteLike(this.article.art_id.toString())
            this.article.attitude = -1
          } else {
            // 点击时, 要进行点赞请求
            await reqAddLike(this.article.art_id.toString())
            this.article.attitude = 1
          }
          this.$toast.success('操作成功')
        } catch (e) {
          this.$toast.success('操作失败')
        }
      }
    }
  }
</script>

<style lang='scss' scoped>
  .article-container {
    position: absolute;
    left: 0;
    top: 0;
    overflow-y: scroll;
    width: 100%;
    height: 100%;
  }

  .article-loading {
    padding-top: 100px;
    text-align: center;
  }

  .error {
    padding-top: 100px;
    text-align: center;
  }

  .detail {
    padding: 50px 10px;

    .title {
      font-size: 16px;
    }

    .zan {
      text-align: center;
    }

    .author {
      padding: 10px 0;
      display: flex;

      .text {
        flex: 1;
        padding-left: 10px;
        line-height: 1.3;

        .name {
          font-size: 14px;
          margin: 0;
        }

        .time {
          margin: 0;
          font-size: 12px;
          color: #999;
        }
      }
    }

    .content {
      font-size: 14px;
      overflow: hidden;
      white-space: pre-wrap;
      word-break: break-all;

      ::v-deep img {
        max-width: 100%;
        background: #f9f9f9;
      }
    }
  }
</style>
